<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/1
 * Time: 16:26
 */

namespace app\admin\controller;
use think\Request;
use  app\admin\traits\ValidateTrait;
use think\exception\ValidateException;
class ManualBook extends Base
{
    use ValidateTrait; // 使用 Trait
    ##
    protected $request;
    protected $service;
    protected $operator_id;
    protected $validationScenes  = [
        'index'=>[
            'pageIndex'=>['require|integer', '页码'],
            'pageSize'  => [['require', 'between:10,100'], '每页数量'],
            'type' => ['integer', '类型'],
        ],
        'create'=>[
            'pids'=>['require','适用矿机ID'],
            'file_id'=>['require|string', '手册文件ID'],
            'type'=>['require|integer', '类型'],
        ],
        'update'=>[
            'id'=>['require|integer', 'id'],
            'pids'=>['require','适用矿机ID'],
            'file_id'=>['require|string', '手册文件ID'],
            'type'=>['require|integer', '类型'],
        ],
        'delete'=>[
            'ids'=>['require|array', 'id'],
        ],
        'show'=>[
            'id'=>['require|integer', 'id'],
        ],
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\ManualBook();

        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 获取员工信息列表
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('index');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }

        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $type   = isset($params['type'])    ? intval($params['type']) : 1;
        $where = ['is_del' => 0];
        if($type){
            $where[] = ['type', '=', $type];
        }
        $res = $this->service->getList($where, $pageIndex, $pageSize, $type);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取矿池详情
     * @date: 2024-08-16 03:07:37
     * @param [int] $id 物流公司ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('show');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 创建矿池
     * @author: Bobo
     * @date: 2024-08-16 03:09:17
     * @param [array] $data 创建物流公司的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $params = $this->request->param();
        try {
            // 根据type判断title是否必填
            if ($params['type'] == 1 && empty($params['title'])) {
                throw new ValidateException('当类型为1时，标题必填');
            }

            // 如果type为2，设置默认title
            if ($params['type'] == 2) {
                $params['title'] = '认证';
            }

            $this->autoValidate('create', $params);
        } catch (ValidateException $e) {
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }

        $params['creator'] = $this->operator_id;
        $result = $this->service->create($params);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 更新矿池
     * @date: 2024-08-16 03:11:17
     * @param [int] $id 物流公司ID @required
     * @param [array] $data 更新物流公司的数据 @required
     * @return Renderable
     */
    public function update()
    {
        $params = $this->request->param();

        try {
            // 根据type判断title是否必填
            if ($params['type'] == 1 && empty($params['title'])) {
                throw new ValidateException('标题必填');
            }

            // 如果type为2，设置默认title
            if ($params['type'] == 2) {
                $params['title'] = '认证';
            }

            $this->autoValidate('update', $params);
        } catch (ValidateException $e) {
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }

        $params['updater'] = $this->operator_id;
        $params['update_time'] = time();
        $result = $this->service->update($params);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 删除矿池
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('delete');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $res = $this->service->delete($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-11 17:02:30
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum(Request $request)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $this->service->getFieldEnum($param);
        return json($arrData);
    }


}
